<template>
  <div class="filtratePage_box">
    <!--考试选择 标题导航栏-->
    <TitleNavbar class="top_navbar">
      <template v-slot:left>
        <svg class="xiangzuo_icon" aria-hidden="true" @click="handleBack">
          <use xlink:href="#icon-xiangzuo"></use>
        </svg>
      </template>
      <template v-slot:center>考试选择</template>
    </TitleNavbar>
    <!--考试选择分类列表-->
    <div class="select_box">
      <!-- 大分类 -->
      <van-tree-select
        v-model:main-active-index="activeIndex"
        height="100%"
        :items="selectList"
        @click-nav="handleSelect"
      >
        <template #content>
          <van-index-bar
            :index-list="indexList"
            highlight-color="red"
            @change="HandleindexBarChange"
            ref="scrollTo"
          >
            <div v-for="item in selectList" :key="item.id">
              <!-- 大分类标题 -->
              <van-index-anchor :index="item.id">
                {{ item.text }}
              </van-index-anchor>
              <!-- 小分类标题 -->
              <div class="xiao_title">
                <div
                  class="xiao_title_item"
                  v-for="item2 in item.childrenTwo"
                  :key="item2.text"
                  @click="handleSelectKind(item, item2)"
                >
                  <svg class="back_icon" aria-hidden="true">
                    <use xlink:href="#icon-kongbaiye"></use>
                  </svg>
                  <!-- 已选状态 -->
                  <svg
                    class="hove_icon icon"
                    aria-hidden="true"
                    v-if="selectStatus(item2.id)"
                  >
                    <use xlink:href="#icon-duigouxiao"></use>
                  </svg>
                  <span>{{ item2.text }}</span>
                </div>
              </div>
            </div>
          </van-index-bar>
        </template>
      </van-tree-select>
    </div>
  </div>
</template>

<script>
import { ref } from "@vue/reactivity";
import TitleNavbar from "../titleNavbar/index.vue";
import { computed, inject } from "@vue/runtime-core";
import { useStore } from "vuex";
import TempSelectList from "../../utils/TempSelectList";

export default {
  name: "filtratePage",
  components: {
    TitleNavbar,
  },
  setup() {
    // 状态库
    const store = useStore();
    // 一级选中索引
    const activeIndex = ref(0);
    // 一级索引列表
    const indexList = ref([]);
    // 列表集
    const selectList = ref(TempSelectList); //临时分类数据
    // 把分类数据存入状态库及本地
    store.commit("testKind/getAllKind", selectList);
    for (let index = 0; index < TempSelectList.length; index++) {
      indexList.value.push(index);
    }
    // 修改左侧的高亮选项
    const HandleindexBarChange = (index) => {
      activeIndex.value = index;
    };
    // 以点击左边大标题使右边滚动到对应小标题区
    const scrollTo = ref(null);
    const handleSelect = (index) => {
      scrollTo.value.scrollTo(index);
    };
    // 点击返回图标销毁筛选页面
    const onControlFiltratePage = inject("onControlFiltratePage");
    const handleBack = () => {
      onControlFiltratePage();
    };
    // 选择种类
    const handleSelectKind = (grade1, grade2) => {
      //返回  一级+二级+三级childrenTwo[0]  （三级 默认为childrenTwo[0]）
      console.log(grade2?.childrenThree[0]?.text);
      let obj = [
        { text: grade1.text, id: grade1.id },
        { text: grade2.text, id: grade2.id },
        {
          text: grade2?.childrenThree[0]?.text,
          id: grade2?.childrenThree[0]?.id,
        },
      ];
      store.commit("testKind/selectTestKind", obj);
      onControlFiltratePage();
    };
    // 匹配对应状态样式
    const selectStatus = computed(() => (id) => {
      // 拿取当前三级分类的id
      let temp = store.state.testKind.putKind[1].id;
      return temp === id;
    });
    return {
      selectList,
      activeIndex,
      indexList,
      scrollTo,
      handleSelect,
      HandleindexBarChange,
      handleBack,
      handleSelectKind,
      selectStatus,
    };
  },
};
</script>

<style scoped lang="less">
.hove_icon {
  width: 1rem;
  height: 1rem;
  position: absolute;
  top: 0;
  right: 0;
}

.filtratePage_box {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  padding-top: 2.5rem;
  z-index: 99;
  background-color: #fff;
  .top_navbar {
    height: 2.5rem;
    width: 100%;
    position: fixed;
    z-index: 99;
    top: 0;
    left: 0;
    background-color: #fff;
  }
}
.xiangzuo_icon {
  width: 1.8rem;
  height: 1.8rem;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
.select_box {
  width: 100%;
  height: 100%;
}
:deep(.van-tree-select) {
  height: 100%;
  overflow-y: hidden;
  .van-sidebar,
  .van-sidebar-item {
    background-color: #fff;
  }
  .van-tree-select__content,
  .van-sidebar-item--select {
    background-color: #fbfbfb;
  }
}
:deep(.van-index-bar::-webkit-scrollbar) {
  display: none;
}

:deep(.van-index-bar__sidebar) {
  display: none;
}

:deep(.van-index-anchor) {
  width: 100%;
  height: 2rem;
  padding: 0 0.5rem;
  line-height: 2rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: #454545;
  box-sizing: border-box;
  background-color: #fff;
  z-index: 0;
}
:deep(.van-index-anchor--sticky) {
  top: -1px;
}

.xiao_title {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .xiao_title_item {
    position: relative;
    width: 40%;
    height: 4rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-around;
    background-color: #fff;
    margin: 0.5rem;
    box-sizing: border-box;
    // border: 1px solid red;
    span {
      font-size: 0.65rem;
      color: #202020;
    }
  }
}
</style>
